<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;

class Transfer extends Base
{
    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'uid')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function take(): object
    {
        return $this->hasOne(User::class, 'id', 'take_uid')
            ->field('id,code,nickname,mobile,photo');
    }

    protected function getAccountAttr($value): string
    {
        return get_pay_way($value);
    }

    public static function add($param, $user, $system): array
    {
        parent::startTrans();
        try {
            if (empty($system['transfer_open'])) {
                throw new \Exception('转账功能已关闭');
            }
            if (!empty($system['transfer_min'])) {
                if ($system['transfer_min'] > $param['amount']) {
                    throw new \Exception('最低转账金额：' . $system['transfer_min']);
                }
            }
            if (!empty($system['transfer_bs']) && $system['transfer_bs'] > 1) {
                if (intval($param['amount']) % intval($system['transfer_bs']) != 0) {
                    throw new \Exception('转账倍数：' . $system['transfer_bs']);
                }
            }
            if ($param['take'] == $user->code) {
                throw new \Exception('收款会员不能为自己');
            }
            //查询账号是否存在
            $take_uid = User::where(['code' => $param['take']])->value('id');
            if (empty($take_uid)) {
                throw new \Exception('收款会员有误，请检查');
            }
            //未开启跨线转账
            if (empty($system['transfer_cross_line'])) {
                //查询是否为团队
                $up = Relation::where(['uid' => $user->id, 'new_uid' => $take_uid])->count();
                if (!empty($system['transfer_lower_open'])) {
                    //开启上下级转账
                    $next = Relation::where(['uid' => $take_uid, 'new_uid' => $user->id])->count();
                    if (!$up && !$next) {
                        throw new \Exception('对方与您非同一团队，无法转账');
                    }
                } else {
                    //未开启上下级转账
                    if (!$up) {
                        throw new \Exception('对方与您非同一团队，无法转账');
                    }
                }
            }
            if (md5($param['pay_password']) != $user->pay_password) {
                throw new \Exception('支付密码错误');
            }
            $account = $param['account'];
            if ($param['amount'] > $user->assets->$account) {
                throw new \Exception(get_pay_way($account) . '账户余额不足');
            }
            $cost = 0;
            if (!empty($system['transfer_cost']) && $system['transfer_cost'] > 0) {
                $cost = round($param['amount'] * $system['transfer_cost'], 2);
            }
            $real_amount = round($param['amount'] - $cost, 2);
            parent::create([
                'account' => $account,
                'uid' => $user->id,
                'take_uid' => $take_uid,
                'amount' => $param['amount'],
                'cost' => $cost,
                'real_amount' => $real_amount,
                'status' => 2
            ]);
            Target::Account()->basics($user->id, $account, $param['amount'], '转账', '-')
                ->createNormal($take_uid);
            Target::Account()->basics($take_uid, $account, $real_amount, '收款', '+')
                ->createNormal($user->id);
            parent::commit();

            return ['code' => 1, 'msg' => '转出成功'];
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}